﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTicino.BtCommLib.Mappers;
using BTicino.BtCommLib.Attributes;
using BTicino.BtCommLib.Exceptions;

namespace BTicino.BtCommLib.Test
{
    /// <summary>
    /// This class is used to translate from high level language to BTicino "WHAT"
    /// code parameter and viceversa.
    /// </summary>
    public class MyWhatMapper : IMapper
    {
        // Dictionary from high level to BTicino low level messages, for Lighting system
        private Dictionary<string, string> dictToLowLevel_Light = new Dictionary<string, string>();
        // Dictionary from BTicino low level to high level messages, for Lighting system
        private Dictionary<string, string> dictToHighLevel_Light = new Dictionary<string, string>();

        // Dictionary from high level to BTicino low level messages, for Automation system
        private Dictionary<string, string> dictToLowLevel_Automation = new Dictionary<string, string>();
        // Dictionary from BTicino low level to high level messages, for Automation system
        private Dictionary<string, string> dictToHighLevel_Automation = new Dictionary<string, string>();

        // Dictionary from high level to BTicino low level messages, for Themo system
        private Dictionary<string, string> dictToLowLevel_Termo = new Dictionary<string, string>();
        // Dictionary from BTicino low level to high level messages, for Thermo system
        private Dictionary<string, string> dictToHighLevel_Termo = new Dictionary<string, string>();

        public MyWhatMapper()
        {
            // LIGHT
            dictToLowLevel_Light.Add("ON", "1");
            dictToLowLevel_Light.Add("OFF", "0");
            // AUTOMATION
            dictToLowLevel_Automation.Add("STOP", "0");
            dictToLowLevel_Automation.Add("UP", "1");
            dictToLowLevel_Automation.Add("DOWN", "2");
            //TERMO
            dictToLowLevel_Termo.Add("ANTIICE", "102");
            //TODO: Complete

            dictToHighLevel_Light.Add("1", "ON");
            dictToHighLevel_Light.Add("0", "OFF");
            //AUTOMATION
            dictToHighLevel_Automation.Add("0", "STOP");
            dictToHighLevel_Automation.Add("1", "UP");
            dictToHighLevel_Automation.Add("2", "DOWN");
            //TERMO
            dictToHighLevel_Termo.Add("102", "ANTIICE");

        }

        /// <summary>
        /// Traslator from high level message to low level message. For specific messages it need the
        /// HighLevelCtxAttr object to discriminate correct value 
        /// </summary>
        /// <param name="key">Key string, it allows to find low level message associate to specifi high level message</param>
        /// <param name="ctxAttr">Object contains other frame information (eg. WHO, WHAT, etc...)</param>
        /// <returns>Low level message</returns>
        public string GetValueToLowLevel(string key, HighLevelCtxAttr ctxAttr)
        {
            string retMsg = string.Empty;
            if (ctxAttr.GetCtxAttr(BTAttributeName.Who).Equals(WhoValues.Light.ToString()))
            {
                if(dictToLowLevel_Light.ContainsKey(key))
                    return dictToLowLevel_Light[key];
                else
                    throw new LibException("What not find");
            }
            else if (ctxAttr.GetCtxAttr(BTAttributeName.Who).Equals(WhoValues.Automation.ToString()))
            {
                if (dictToLowLevel_Automation.ContainsKey(key))
                    return dictToLowLevel_Automation[key];
            }
            else if (ctxAttr.GetCtxAttr(BTAttributeName.Who).Equals(WhoValues.Termo.ToString()))
            {
                if (dictToLowLevel_Termo.ContainsKey(key))
                    return dictToLowLevel_Termo[key];
            }
            throw new LibException("WHO not find");
        }

        /// <summary>
        /// Traslator from low level message to high level message. For specific messages it need the
        /// LowLevelCtxAttr object to discriminate correct value 
        /// </summary>
        /// <param name="key">Key string, it allows to find high level message associate to specifi low level message</param>
        /// <param name="ctxAttr">Object contains other frame information (eg. WHO, WHAT, etc...)</param>
        /// <returns>High level message</returns>
        public string GetValueToHighLevel(string key, LowLevelCtxAttr ctxAttr)
        {
            Console.WriteLine("WHO: " + WhoValues.Automation.GetHashCode().ToString() + " Position: " + Enum.GetName(typeof(WhoValues), WhoValues.Light));
            if (ctxAttr.GetCtxAttr(BTAttributeName.Who).Equals(WhoValues.Light.GetHashCode().ToString()))
            {
                return dictToHighLevel_Light[key];
            }
            else if (ctxAttr.GetCtxAttr(BTAttributeName.Who).Equals(WhoValues.Automation.GetHashCode().ToString()))
            {
                return dictToHighLevel_Automation[key];
            }
            else if (ctxAttr.GetCtxAttr(BTAttributeName.Who).Equals(WhoValues.Termo.GetHashCode().ToString()))
            {
                return dictToHighLevel_Termo[key];
            }
            throw new LibException("What not find");
        }
    }
}